package com.base.mine.presenter;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.callback.NavCallback;
import com.alibaba.android.arouter.launcher.ARouter;
import com.base.commonlib.base.BaseApp;
import com.base.commonlib.base.presenter.BasePresenter;
import com.base.commonlib.bean.User;
import com.base.commonlib.http.RealNetWorkUtil;
import com.base.commonlib.http.utils.CallBackImpl;
import com.base.commonlib.sp.SpUtil;
import com.base.commonlib.utils.RxBus2;
import com.base.commonlib.utils.SystemUtil;
import com.base.commonlib.widget.CommonDialog;
import com.base.mine.R;
import com.base.mine.bean.Version;
import com.base.mine.config.RouteConfig;
import com.base.mine.view.ForgetPwdView;
import com.base.mine.view.PhoneLoginView;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.disposables.Disposable;

/**
 * Created by weikailiang on 2019/9/18.
 */

public class ForgetPwdPresenter extends BasePresenter<ForgetPwdView>{
    private int time = 60;
    public String sendPhone = "";
    public ForgetPwdPresenter(ForgetPwdView view, Activity activity) {
        super(view, activity);
    }

    @Override
    public void start() {
    }




    public void SendCode(final String phone){
        if (phone.length()!=11){
            mView.showMes("请输入正确的手机号");
            return;
        }
        mView.showPb_mes("正在发送短信");
        HashMap<String, Object> param = new HashMap<>();
        param.put("phone",phone);
        Disposable disposable = RealNetWorkUtil.get()
                .method("api-auth/user/sendMsg")
                .params(param)
                .build().enqueue(String.class, new CallBackImpl<String>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        mView.hidePb_mes();
                        if (!TextUtils.isEmpty(msg)) {
                            mView.showMes(msg);
                        }
                    }
                    @Override
                    public void onNext(String version) {
                        mView.hidePb_mes();
                        sendPhone = phone;
                        time = 60;
                        mView.returnSendCodeView().setText(time+"");
                        mView.returnSendCodeView().setTextColor(ContextCompat.getColor(mActivity,R.color.color_tab_text_normal));
                        mHandler.sendEmptyMessageDelayed(SEND_MESSAGE,1000);
                    }
                });
        mCompositeDisposable.add(disposable);
    }
    private static final int SEND_MESSAGE = 1;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case SEND_MESSAGE:
                    if (time==0){
                        mView.returnSendCodeView().setText("发送验证码");
                        mView.returnSendCodeView().setTextColor(ContextCompat.getColor(mActivity,R.color.color_4486FF));
                    }else {
                        time--;
                        mView.returnSendCodeView().setText(time + "");
                        mHandler.sendEmptyMessageDelayed(SEND_MESSAGE, 1000);
                    }
                    break;
            }
        }
    };


    /**
     * 修改密码
     * @param phone
     * @param code
     * @param password
     */
    public void updatePwdByPhone(final String phone, String code,String password){
        if (phone.length()!=11){
            mView.showMes("请输入正确的手机号");
            return;
        }
        if (TextUtils.isEmpty(code)){
            mView.showMes("请输入验证码");
            return;
        }
        if (code.length()<4){
            mView.showMes("请输入正确验证码");
            return;
        }
        mView.showPb_mes("正在提交。。。");
        HashMap<String, String> param = new HashMap<>();
        param.put("phone",phone);
        param.put("code",code);
        param.put("password",password);
        Disposable disposable = RealNetWorkUtil.postContent()
                .method("api-auth/user/forgetPassword")
                .content(param)
                .build().enqueue(String.class, new CallBackImpl<String>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        mView.hidePb_mes();
                        if (!TextUtils.isEmpty(msg)) {
                            mView.showMes(msg);
                        }
                    }
                    @Override
                    public void onNext(final String msg) {
                        mView.hidePb_mes();
                        mView.showMes("修改成功");
                        ARouter.getInstance().build("/mine/pwdlogin").navigation(mActivity, new NavCallback() {
                            @Override
                            public void onArrival(Postcard postcard) {
                                SpUtil.setUserPw("");
                                mActivity.finish();
                            }
                        });

                    }
                });
        mCompositeDisposable.add(disposable);
    }
    /**
     * 积分
     */
    public void getJifeng(final User user, final boolean isNeedPwd){
//        mView.showPb();
        Map<String, Object> map = new HashMap<>();
        map.put("settingKey","08020003");
        map.put("funcMainId", "edu");
        Disposable enqueue = RealNetWorkUtil.postContent().method("api-function/integral/save").content(map)
                .build().enqueue(String.class, new CallBackImpl<String>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
//                        mView.hidePb();
                    }

                    @Override
                    public void onNext(String str) {
//                        mView.hidePb();
                        mView.showMes(str);
                        if (isNeedPwd) {
                            if (TextUtils.isEmpty(user.getTenantId()) && TextUtils.isEmpty(user.getOrgId()) && TextUtils.isEmpty(user.getDeptId())) {
                                ARouter.getInstance().build("/mail/structure").navigation();
                            } else {
//                        SpUtil.setUserPw();
                                ARouter.getInstance().build("/study/home").withInt("type",1).withInt("num",2).navigation(mActivity, new NavCallback() {
                                    @Override
                                    public void onArrival(Postcard postcard) {
                                        mActivity.finish();
                                    }
                                });
                            }
                        }else {
                            if (TextUtils.isEmpty(user.getTenantId()) && TextUtils.isEmpty(user.getOrgId()) && TextUtils.isEmpty(user.getDeptId())) {
                                ARouter.getInstance().build("/mail/structure").navigation();
                            } else {
//                        SpUtil.setUserPw();
                                ARouter.getInstance().build("/study/home").withInt("num",2).navigation(mActivity, new NavCallback() {
                                    @Override
                                    public void onArrival(Postcard postcard) {
                                        mActivity.finish();
                                    }
                                });
                            }
                        }
                    }
                });

        mCompositeDisposable.add(enqueue);
    }


    @Override
    public void release() {
        super.release();
        if (mHandler!=null) {
            mHandler.removeCallbacksAndMessages(null);
        }
    }
}
